package com.yc.hr.ui;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JSpinner;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class PagePanel extends JPanel {
	
	private int rows = 10;  // 每页行数 要设置
	private int total;      // 总行数  要查询
	private int page = 1;   // 当前页
	private int pages;      // 总页数  要计算
	private JLabel pageLabel;
	private JButton firstBtn;
	private JButton preBtn;
	private JButton nextBtn;
	private JButton lastBtn;
	private JSpinner pageSpn;
	
	// 定义响应page更改事件的对象(事件监听器)
	private PageChangeListener pageChangeListener;

	/**
	 * Create the panel.
	 */
	public PagePanel() {
		FlowLayout flowLayout = (FlowLayout) getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		
		firstBtn = new JButton("第一页");
		add(firstBtn);
		
		preBtn = new JButton("上一页");
		
		add(preBtn);
		
		pageSpn = new JSpinner();
		
		pageSpn.setPreferredSize(new Dimension(50, 22));
		add(pageSpn);
		
		// 局部变量升级为全局变量, 一定要将局部变量的类型去掉
		pageLabel = new JLabel("页 / 共0页");
		add(pageLabel);
		
		nextBtn = new JButton("下一页");
		
		add(nextBtn);
		
		lastBtn = new JButton("最后一页");
		
		add(lastBtn);
		
		//////////////////////////////////////////////////////////
		firstBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setPage(1);
			}
		});
		preBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(page > 1) {
					setPage(page-1);
				}
			}
		});
		nextBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(page < pages) {
					setPage(page+1);
				}
			}
		});
		lastBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setPage(pages);
			}
		});
		pageSpn.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				// 如果总页数是0, 则返回
				if(pages==0)return;
				int p = Integer.valueOf(pageSpn.getValue().toString());
				if(p < 1) {
					pageSpn.setValue(1);
				} else if(p > pages) {
					pageSpn.setValue(pages);
				} else {
					setPage(p);
				}
				if(pageChangeListener!=null) {
					// 触发 page 更改的事件
					pageChangeListener.change(page);
				}
			}
		});

	}
	
	////////////////////////////////////////////////////////////////
	// 根据当前的数值, 刷新界面元素
	public void refresh() {
		this.pages = this.total / this.rows;
		if(this.total % this.rows != 0) {
			this.pages ++;
		}
		firstBtn.setEnabled(page != 1);
		preBtn.setEnabled(page != 1);
		nextBtn.setEnabled(page != pages);
		lastBtn.setEnabled(page != pages);
		this.pageSpn.setValue(this.page);
		pageLabel.setText("页 / 共" + this.pages + "页");
	}
	
	// page 更改的监听器
	public static interface PageChangeListener {
		void change(int page);
	}
	
	//-------------------- 自动生成 get set 方法 -----------------------//

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
		refresh();
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
		refresh();
//		if(pageChangeListener!=null) {
//			// 触发 page 更改的事件
//			pageChangeListener.change(page);
//		}
		
	}

	public int getPages() {
		return pages;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
		refresh();
	}
	
	public void setPageChangeListener(PageChangeListener pageChangeListener) {
		this.pageChangeListener = pageChangeListener;
	}


}
